<?php

class Usermodel extends CI_Model {

    //add a role
    public function add_role($role_name = "") {
        $save_role = array(
            'role' => $role_name
        );

        $save = $this->db->insert('role', $save_role);
        return $save;
    }

    //update a role
    public function update_role($role_id = null, $role_name = null) {
        if ($role_id !== null) {

            $stack = array();

            if ($role_name !== null) {
                $stack['role'] = $role_name;
            }

            if ($stack !== null) {
                $this->db->where('role_id', $role_id);
                return $this->db->update('role', $stack);
            }
        }
    }

    //get all data role
    public function get_all_role($where = null, $order_by = null, $limit = 10000, $offset = 0) {
        $this->db->from('role');

        if ($where !== null)
            $this->db->where($where);
        if ($order_by !== null)
            $this->db->order_by($order_by);

        $this->db->limit($limit);
        $this->db->offset($offset);

        $query = $this->db->get();

        return $query;
    }

    //get info from a role id
    public function get_info_role($role_id = null) {
        if ($role_id !== null) {
            return $this->get_all_role(array('role_id' => $role_id));
        }
    }

    //delete a role
    public function delete_role($role_id = null) {
        if ($role_id !== null)
            $this->db->delete('role', array('role_id' => $role_id));
        return $this->db->affected_rows();
    }

    //delete table role
    public function delete_table_role() {
        return $this->db->delete('role');
    }

    //empty table role
    public function empty_table_role() {
        $this->db->empty_table('role');
    }

    //add a user
    public function add_user($username = null, $password = null, $email = null, $role_id = null, $plasa_id = null) {
        $add_user = array(
            'username' => $username,
            'password' => md5($password),
            'email' => $email,
            'role_id' => $role_id,
            'plasa_id' => $plasa_id
        );

        $this->add_user_arr($add_user);
    }

    public function add_user_arr($user_tuple = null) {
        if ($user_tuple !== null) {
            $save = $this->db->insert('user', $user_tuple);
            return $save;
        }
    }

    //update a role
    public function update_user($user_id = null, $username = null, $password = null, $email = null, $role_id = null, $plasa_id = null) {

        if ($user_id !== null) {

            $stack = array();

            if ($username !== null) {
                $stack['username'] = $username;
            }

            if ($password !== null) {
                $stack['password'] = md5($password);
            }

            if ($email !== null) {
                $stack['email'] = $email;
            }

            if ($role_id !== null) {
                $stack['role_id'] = $role_id;
            }
            
            if ($plasa_id !== null) {
                $stack['plasa_id'] = $plasa_id;
            }

            if ($stack !== null) {
                print_r($stack);
                return $this->update_user_arr($user_id, $stack);
            }
        }
    }

    public function update_password_user($user_id = null, $new_password = null, $old_password = null, $is_admin = TRUE) {
        if ($user_id !== null) {
            $old_user = $this->get_info_user($user_id);

            foreach ($old_user->result() as $row) {
                if ($is_admin) {
                    return $this->update_user($user_id, null, $new_password);
                } else {
                    if ($row->password == md5($old_password)) {
                        echo $new_password . ' ';
                        return $this->update_user($user_id, null, $new_password);
                    } else
                        return '0';
                }
            }
        }
    }

    //update user by an array of user information
    public function update_user_arr($user_id = null, $update_tuple = null) {
        if (($user_id !== null) && ($update_tuple !== null)) {
            $this->db->where('user_id', $user_id);
            return $this->db->update('user', $update_tuple);
        }
    }

    //delete a user
    public function delete_user($user_id = null) {
        if ($user_id !== null)
            $this->db->delete('user', array('user_id' => $user_id));
        return $this->db->affected_rows();
    }

    //delete table user
    public function delete_table_user() {
        return $this->db->delete('user');
    }

    //empty table user
    public function empty_table_user() {
        $this->db->empty_table('user');
    }

    //get all data user
    public function get_all_user($where = null, $order_by = null, $limit = 10000, $offset = 0) {

        $this->db->select('user.*, role.*', FALSE);
        $this->db->from('user');

        if ($where !== null)
            $this->db->where($where);

        $this->db->join('role', 'user.role_id = role.role_id', 'INNER JOIN');

        if ($order_by !== null)
            $this->db->order_by($order_by);

        $this->db->limit($limit);
        $this->db->offset($offset);

        return $this->db->get();
    }

    public function get_all_csr_by_plasa($plasa_id) {
        if ($plasa_id != null) {
            $this->db->select('csr.csr_id AS csr_id, csr.name AS csr_name, csr.contact AS csr_contact, csr.plasa_id AS plasa_id, plasa.name AS plasa_name');
            $this->db->from('csr');
            $this->db->where('csr.plasa_id', $plasa_id);
            $this->db->join('plasa', 'csr.plasa_id = plasa.plasa_id', 'INNER JOIN');
            $query = $this->db->get();
            //return $this->db->last_query();
            return $query;
        }
    }

    public function get_all_user_by_super_admin($order_by = null, $limit = 10000, $offset = 0) {

        $this->db->select('user.user_id AS user_id,user.username AS username,user.email AS email ,role.role AS role, plasa.name AS plasa_name', FALSE);
        $this->db->from('user');

        $this->db->where('user.role_id !=', '0');

        $this->db->join('role', 'user.role_id = role.role_id', 'INNER JOIN');
        $this->db->join('plasa', 'user.plasa_id = plasa.plasa_id', 'INNER JOIN');

        if ($order_by !== null) {
            $this->db->order_by($order_by);
        }

        $this->db->limit($limit);
        $this->db->offset($offset);

        //return $this->db->last_query();
        $result = $this->db->get();
        //return $this->db->last_query();
        return $result;
    }

    public function get_all_user_by_plasa($plasa_id = null, $order_by = null, $limit = 10000, $offset = 0) {
        if ($plasa_id != null) {
            $this->db->select('user.user_id AS user_id,user.username AS username,user.email AS email ,role.role AS role, plasa.name AS plasa_name', FALSE);
            $this->db->from('user');

            $this->db->where('user.role_id !=', '0');
            $this->db->where("user.plasa_id", $plasa_id);

            $this->db->join('role', 'user.role_id = role.role_id', 'INNER JOIN');
            $this->db->join('plasa', 'user.plasa_id = plasa.plasa_id', 'INNER JOIN');

            if ($order_by !== null) {
                $this->db->order_by($order_by);
            }

            $this->db->limit($limit);
            $this->db->offset($offset);

            //return $this->db->last_query();
            $result = $this->db->get();
            //return $this->db->last_query();
            return $result;
        }
    }
    
    public function get_all_user_by_super_admin_2($order_by = null, $limit = 10000, $offset = 0) {

        $this->db->select('user.user_id AS user_id,user.username AS username,user.email AS email ,role.role AS role, plasa.name AS plasa_name', FALSE);
        $this->db->from('user');

        $this->db->where('user.role_id !=', '0');

        $this->db->join('role', 'user.role_id = role.role_id', 'INNER JOIN');
        $this->db->join('plasa', 'user.plasa_id = plasa.plasa_id', 'INNER JOIN');

        if ($order_by !== null) {
            $this->db->order_by($order_by);
        }

        $this->db->limit($limit);
        $this->db->offset($offset);

        //return $this->db->last_query();
        $result = $this->db->get();
        //return $this->db->last_query();
        return $result;
        //return "bangke";
    }

    //count number of record
    public function count_all_user() {
        $this->db->from('user');
        $query = $this->db->count_all_results();

        return $query;
    }

    //get info from a user id
    public function get_info_user($userid = null) {

        if ($userid !== null) {
            return $this->get_all_user(array('user_id' => $userid), null, 1);
        }
    }

    //login checking
    public function is_login_correct($username = null, $password = null) {
        if ($username != null && $password != null) {
            $this->db->where('username', $username);
            $this->db->where('password', md5($password));
            $query = $this->db->get('user');
            if ($query->num_rows() == 1) {
                return 1;
            } else {
                return 0;
            }
        } else {
            return 0;
        }
    }

    //get_role
    public function get_user_role($username = null) {
        if ($username != null) {
            $this->db->where('username', $username);
            $query = $this->db->get('user');
            return $query->row()->role_id;
        }
    }

    //get_plasa_id
    public function get_plasa_id_by_userid($user_id = null) {
        if ($user_id != null) {
            $this->db->where('user_id', $user_id);
            $query = $this->db->get('user');
            return $query->row()->plasa_id;
        }
    }

    public function get_plasa_id_by_username($username = null) {
        if ($username != null) {
            $this->db->where('username', $username);
            $query = $this->db->get('user');
            return $query->row()->plasa_id;
        }
    }

    public function get_user_id($username = null) {
        if ($username != null) {
            $this->db->where('username', $username);
            $query = $this->db->get('user');
            return $query->row()->user_id;
        }
    }

}

?>
